package com.bee.baselibrary.weight;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bee.baselibrary.R;


public class BottomBarView extends RelativeLayout {

    ImageView barIv;
    TextView barTv;
    TextView barNum;
    private static int defaultTitleTextColor;

    private int msgCount;
    private Context mContext;
    private Drawable imgRes;
    private String text;
    private int textColor;


    public BottomBarView(Context context) {
        super(context);
        this.mContext = context;
    }

    public BottomBarView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        this.mContext = context;
        this.getAtty(attrs);
    }

    public BottomBarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        RelativeLayout rl = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.superscript_view_layout, this, true);
        barNum = (TextView) rl.findViewById(R.id.bar_num);
        barTv = (TextView) rl.findViewById(R.id.bar_tv);
        barIv = (ImageView) rl.findViewById(R.id.bar_iv);
        this.getAtty(attrs);
        this.initLayout();
    }


    private void getAtty(AttributeSet attrs){
        TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.bottom_bar);
        imgRes = typedArray.getDrawable(R.styleable.bottom_bar_l_imgRes);
        text = typedArray.getString(R.styleable.bottom_bar_l_Text);
        textColor = typedArray.getColor(R.styleable.bottom_bar_l_TextColor,defaultTitleTextColor);
        typedArray.recycle();
    }

    private void initLayout(){
        if (TextUtils.isEmpty(text)){
            barTv.setText(text);
        }
        if (imgRes!=null){
            barIv.setImageDrawable(imgRes);
        }
        if (textColor!=0){
            barTv.setTextColor(textColor);
        }
    }
    public void setMessageCount(int count) {
        msgCount = count;
        if (count == 0) {
            barNum.setVisibility(View.GONE);
        } else {
            barNum.setVisibility(View.VISIBLE);
            if (count < 100) {
                barNum.setText(count + "");
            } else {
                barNum.setText("99+");
            }
        }
        invalidate();
    }


    public void addMsg() {
        setMessageCount(msgCount + 1);
    }

}